package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.dao.ColumnDAO;
import com.junzhiit.nengji.model.Column;

@Repository
public class ColumnDAOImpl extends AbstractBaseDAO<Column> implements ColumnDAO {
	
	@Override
	public List<Column> findAll(Column column,int pageNumber, int maxPageRow) {
		StatementParameter param=new StatementParameter();
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from `column`");
		sql.append(" where is_delete=?");
		param.setInt(IsDelete.NO.toIntValue());
		if(StringUtils.isNotBlank(column.getShopId())){
			sql.append(" and shop_id = ? ");
			param.setString(column.getShopId());
		}else{
			sql.append(" and shop_id is null ");
		}
		sql.append(" order by code asc");
		
		return super.queryForPagingList(sql.toString(), Column.class, param, pageNumber, maxPageRow);

	}

	@Override
	public int getTotalCount(Column column) {
		StringBuffer sql = new StringBuffer();
		StatementParameter param = new StatementParameter();
		sql.append(" select count(*) from `column`");
		sql.append(" where is_delete=? ");
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(column.getShopId())){
			sql.append(" and shop_id = ? ");
			param.setString(column.getShopId());
		}else{
			sql.append(" and shop_id is null ");
		}
		
		return super.queryForInt(sql.toString(), param);
	}
	
	@Override
	public int getTotalModulesCount(String columnId){
		StringBuffer sql=new StringBuffer();
		sql.append(" select count(*) from `modules`");
		sql.append(" where is_delete=? and column_id=?");
		
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(columnId);
		
		return super.queryForInt(sql.toString(),param);
	}
	
	@Override
	public List<Column> findNoPageAll(Column column){
		StringBuffer sql = new StringBuffer();
		StatementParameter param = new StatementParameter();
		sql.append(" select * from `column`");
		sql.append(" where is_delete=?");
		param.setInt(IsDelete.NO.toIntValue());
		if(StringUtils.isNotBlank(column.getShopId())){
			sql.append(" and shop_id = ? ");
			param.setString(column.getShopId());
		}else{
			sql.append(" and shop_id is null ");
		}
		
		return super.queryForList(sql.toString(),Column.class,param);
	}
	
	@Override
	public String getMaxCode(Column column){
		StringBuffer sql = new StringBuffer();
		StatementParameter param = new StatementParameter(); 
		sql.append("select max(code) from `column` where 1=1 ");
		if(StringUtils.isNotBlank(column.getShopId())){
			sql.append(" and shop_id = ? ");
			param.setString(column.getShopId());
		}else{
			sql.append(" and shop_id is null ");
		}
		return super.queryForString(sql.toString(),param);
	}

	@Override
	public Column getColumnByCode(String code,String shopId) {
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from `column` where is_delete=? and code=?");
	
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(code);
		
		if(StringUtils.isNotBlank(shopId)){
			sql.append(" and shop_id = ? ");
			param.setString(shopId);
		}else{
			sql.append(" and shop_id is null ");
		}
		
		return query(sql.toString(),Column.class,param);
	}

}
